//
//  MAADMnager.m
//  MateAdv
//
//  Created by 茶 on 2023/9/20.
//

#import "CAADManager.h"
#import "CANavController.h"

#import <BUAdSDK/BUAdSDK.h>

@interface CAADManager () <BUSplashAdDelegate, BUNativeExpressBannerViewDelegate>

@property (nonatomic, assign, readwrite) BOOL isOpeningShow;

@property (nonatomic, strong) BUSplashAd *splashAd; // 开屏
//@property (nonatomic, strong) BUNativeExpressFullscreenVideoAd *fullscreenAd; //插屏
@property (nonatomic, strong) BUNativeExpressBannerView *bannerView;

@end

@implementation CAADManager


- (void)initADManager {
    if (!CAUserObj(kADAppIDKey)) {
        [kUserDefaults setObject:@"5450321" forKey:kADAppIDKey];
        [kUserDefaults setObject:@"888669911" forKey:kADOpeningKey];
        [kUserDefaults setObject:@"954509553" forKey:kADBanerKey];
//        [kUserDefaults setObject:@"954509552" forKey:kADPopKey];
    }
    
    if (kUserModel.vipStatus == NO) {
        BUAdSDKConfiguration *configuration = [BUAdSDKConfiguration configuration];
        configuration.appID = CAUserObj(kADAppIDKey);//除appid外，其他参数配置按照项目实际需求配置即可。
        [BUAdSDKManager startWithAsyncCompletionHandler:^(BOOL success, NSError *error) {
            if (success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    //请求广告逻辑处理
                    if (CAUserBool(kNotFirst)) {
                        [self openADEvent];
                    } else {
                        [kUserDefaults setBool:YES forKey:kNotFirst];
                    }
                });
            }
        }];
    }
}

- (void)openADEvent {
    if (!_splashAd && kUserModel.vipStatus == NO) {
        _isOpeningShow = NO;
        _splashAd = [[BUSplashAd alloc] initWithSlotID:CAUserObj(kADOpeningKey) adSize:[UIScreen mainScreen].bounds.size];
        // 设置开屏广告代理
        _splashAd.delegate = self;
        _splashAd.tolerateTimeout = 3;
        // 加载广告
        [_splashAd loadAdData];
    }
}

//- (void)fullScreenADEvent {
//    if (!_fullscreenAd && kUserModel.vipStatus == NO) {
//        _fullscreenAd = [[BUNativeExpressFullscreenVideoAd alloc] initWithSlotID:CAUserObj(kADPopKey)];
//        _fullscreenAd.delegate = self;
//        [_fullscreenAd loadAdData];
//    }
//}

- (void)bannerAdEventWithController:(UIViewController *)controller {
    if (!_bannerView && kUserModel.vipStatus == NO) {
        CGFloat bannerHeight = CARatioWidth(75);
        CGFloat bannerWidth = CARatioWidth(300);
        
        self.bannerView = [[BUNativeExpressBannerView alloc] initWithSlotID:CAUserObj(kADBanerKey) rootViewController:controller adSize:CGSizeMake(bannerWidth, bannerHeight)];
        self.bannerView.frame = CGRectMake((controller.view.width - bannerWidth) / 2, controller.view.height-bannerHeight, bannerWidth, bannerHeight);
        self.bannerView.delegate = self;
        [self.bannerView loadAdData];
        [controller.view addSubview:_bannerView];
    }
}

#pragma mark - BUSplashAdDelegate

- (void)splashAdDidClick:(nonnull BUSplashAd *)splashAd { 
    
}

- (void)splashAdDidClose:(nonnull BUSplashAd *)splashAd closeType:(BUSplashAdCloseType)closeType {
    CABlock(self.openClose);
}

- (void)splashAdDidShow:(nonnull BUSplashAd *)splashAd {
    self.isOpeningShow = YES;
}

- (void)splashAdLoadFail:(nonnull BUSplashAd *)splashAd error:(BUAdError * _Nullable)error { 
    NSLog(@"=============== %@", error);

    CABlock(self.openClose);
}

- (void)splashAdLoadSuccess:(nonnull BUSplashAd *)splashAd { 
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [splashAd showSplashViewInRootViewController:keyWindow.rootViewController];
}

- (void)splashAdRenderFail:(nonnull BUSplashAd *)splashAd error:(BUAdError * _Nullable)error { 
    NSLog(@"=============== %@", error);
    CABlock(self.openClose);
}

- (void)splashAdRenderSuccess:(nonnull BUSplashAd *)splashAd { 
    
}

- (void)splashAdViewControllerDidClose:(nonnull BUSplashAd *)splashAd { 
    
}

- (void)splashAdWillShow:(nonnull BUSplashAd *)splashAd { 
    
}

- (void)splashDidCloseOtherController:(nonnull BUSplashAd *)splashAd interactionType:(BUInteractionType)interactionType { 
    
}


- (void)splashVideoAdDidPlayFinish:(BUSplashAd *)splashAd didFailWithError:(NSError *)error {
    
}

//#pragma mark - BUNativeExpressFullscreenVideoAdDelegate
//- (void)nativeExpressFullscreenVideoAdDidDownLoadVideo:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
////回调后再去展示广告，可保证播放和展示效果
//    if (_fullscreenAd) {
//       [_fullscreenAd showAdFromRootViewController:UIWindow.currentViewController];
//    }
//}
//- (void)nativeExpressFullscreenVideoAdDidClose:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
//    _fullscreenAd = nil;
//}

#pragma mark - BUNativeExpressBannerViewDelegate

- (void)nativeExpressBannerAdViewRenderSuccess:(BUNativeExpressBannerView *)bannerAdView {
    
}

@end
